package com.gs.SynAntiWandering.activity.insight;

import android.content.Intent;

import com.gs.SynAntiWandering.R;
import com.gs.SynAntiWandering.activity.base.BaseListActivity;
import com.gs.SynAntiWandering.adapter.InsightInOutAdapter;
import com.gs.SynAntiWandering.apiresult.ApiResultData;
import com.gs.SynAntiWandering.apiresult.PagedList;
import com.gs.SynAntiWandering.constant.AllConstants;
import com.gs.SynAntiWandering.control.LinearLayoutManager;
import com.gs.SynAntiWandering.db.dto.InsightInOutDto;
import com.gs.SynAntiWandering.db.query.DashAlertQuery;
import com.gs.SynAntiWandering.presenter.InsightInOutPresenter;
import com.gs.SynAntiWandering.util.ToastUtil;
import com.gs.SynAntiWandering.view.IInsightInOutView;

public class InsightInOutActivity extends BaseListActivity<IInsightInOutView, InsightInOutPresenter, InsightInOutDto, DashAlertQuery> implements IInsightInOutView {

    private InsightInOutAdapter adapter;
    private String serialNo;

    @Override
    public int getLayoutId() {
        return R.layout.activity_insight_list;
    }

    @Override
    public void initView() {
        Intent intent = getIntent();
        serialNo = intent.getStringExtra(AllConstants.ID);
        adapter = new InsightInOutAdapter(this, list, R.layout.rvitem_in_out);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void loadData() {
        mPresenter.getData(query);
    }

    @Override
    public void add() {

    }

    @Override
    public void search() {
        list.clear();
        query.init();
        query.setSerialNo(serialNo);
        mPresenter.getData(query);
    }

    @Override
    public InsightInOutPresenter createPresenter() {
        return new InsightInOutPresenter();
    }

    @Override
    public DashAlertQuery createQuery() {
        return new DashAlertQuery();
    }

    @Override
    public void onGetDataSuccess(ApiResultData<PagedList<InsightInOutDto>> apiResult) {
        refreshSmartRefreshLayout();
        if (apiResult.getStatus() != 2000) {
            ToastUtil.showToast(apiResult.getMessage());
            adapter.notifyDataSetChanged();
            return;
        }
        list.addAll(apiResult.getData().getList());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onGetDataFails(Exception e) {
        refreshSmartRefreshLayout();
        adapter.notifyDataSetChanged();
        ToastUtil.showToast( R.string.network_error);
    }
}